TAREA 6 - Grupo 5 (Japan)¶
Integrantes:
- Palacios Ninahuanca, Ninoska
- Cerda Rioja, Lorena
- Lliuya Saldaña, Brayan
#instalación fiona
!pip install fiona
Collecting fiona
Downloading fiona-1.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (56 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.6/56.6 kB 2.4 MB/s eta 0:00:00
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.10/dist-packages (from fiona) (24.2.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from fiona) (2024.8.30)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.10/dist-packages (from fiona) (8.1.7)
Collecting click-plugins>=1.0 (from fiona)
Downloading click_plugins-1.1.1-py2.py3-none-any.whl.metadata (6.4 kB)
Collecting cligj>=0.5 (from fiona)
Downloading cligj-0.7.2-py3-none-any.whl.metadata (5.0 kB)
Downloading fiona-1.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 76.9 MB/s eta 0:00:00
Downloading click_plugins-1.1.1-py2.py3-none-any.whl (7.5 kB)
Downloading cligj-0.7.2-py3-none-any.whl (7.1 kB)
Installing collected packages: cligj, click-plugins, fiona
Successfully installed click-plugins-1.1.1 cligj-0.7.2 fiona-1.10.1
# SE IMPORTAN LOS ARCHIVOS GPKG PASADOS DE JAPON (tarea 4)
import geopandas as gpd
Japon_Adm = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg")
japon_states = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg", layer="Estados")
japon_municipalities = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg", layer="Municipios")
/usr/local/lib/python3.10/dist-packages/pyogrio/geopandas.py:265: UserWarning: More than one layer found in 'JaponAdm.gpkg': 'Estados' (default), 'Municipios'. Specify layer parameter to avoid this warning. result = read_func(
# SE IMPORTA LA CAPA DE RIOS (tarea 4)
rivers = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/MapasJapon.gpkg", layer="rivers_jp")
# SE IMPORTA LOS AEROPUERTOS DE JAPON
airports=gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej2.gpkg",layer="airports")
# SE IMPORTA EL BORDE DEL PAÍS
border=gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej3.gpkg")
# IMPORTANDO DATA DE PUERTOS A NIVEL MUNDIAL
import pandas as pd
portsFileLink="https://github.com/CienciaDeDatosEspacial/GeoDataFrame_Analytics/raw/main/data/UpdatedPub150.csv"
infoseaports=pd.read_csv(portsFileLink)
infoseaports.head(3)
| World Port Index Number | Region Name | Main Port Name | Alternate Port Name | UN/LOCODE | Country Code | World Water Body | IHO S-130 Sea Area | Sailing Direction or Publication | Publication Link | ... | Supplies - Fuel Oil | Supplies - Diesel Oil | Supplies - Aviation Fuel | Supplies - Deck | Supplies - Engine | Repairs | Dry Dock | Railway | Latitude | Longitude | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7950.0 | United States E Coast -- 6585 | Maurer | United States | North Atlantic Ocean | U.S. Coast Pilot 2 - Atlantic Coast: Cape Cod ... | https://nauticalcharts.noaa.gov/publications/c... | ... | Yes | Yes | Unknown | Yes | Yes | Moderate | Unknown | Unknown | 40.533333 | -74.250000 | |||
| 1 | 52235.0 | Sulawesi -- 51970 | Mangkasa Oil Terminal | Indonesia | Teluk Bone; Banda Sea; South Pacific Ocean | Sailing Directions Pub. 163 (Enroute) - Borneo... | https://msi.geo.nga.mil/api/publications/downl... | ... | No | No | Unknown | No | No | NaN | Unknown | Unknown | -2.733333 | 121.066667 | |||
| 2 | 47620.0 | Madagascar -- 47350 | Iharana | Madagascar | Indian Ocean | Sailing Directions Pub. 171 (Enroute) - East A... | https://msi.geo.nga.mil/api/publications/downl... | ... | No | No | Unknown | No | No | Emergency Only | Unknown | Unknown | -13.350000 | 50.000000 |
3 rows × 108 columns
# SELECCIONAR SOLO COLUMNAS DE INTERÉS
infoseaports.rename(columns={'Main Port Name':'portName'},inplace=True)
infoseaports=infoseaports.loc[:,['portName', 'Country Code','Latitude', 'Longitude']]
infoseaports.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3739 entries, 0 to 3738 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 portName 3739 non-null object 1 Country Code 3739 non-null object 2 Latitude 3739 non-null float64 3 Longitude 3739 non-null float64 dtypes: float64(2), object(2) memory usage: 117.0+ KB
# PROYECTANDO LA DATA
seaports=gpd.GeoDataFrame(data=infoseaports.copy(),
geometry=gpd.points_from_xy(infoseaports.Longitude,
infoseaports.Latitude),
crs=6684)
# separar solo para Japón
seaports_jap=seaports[seaports['Country Code']=='Japan'].copy()
# Reasignando índices
seaports_jap.reset_index(drop=True, inplace=True)
# reprojectando
seaports_jap_6684=seaports_jap.to_crs(6684)
seaports_jap_6684.plot(markersize=3)
<Axes: >
# Seleccionando solo aeropuestos grandes
largeA=airports[airports['kind']=='large_airport']
largeA.reset_index(drop=True, inplace=True)
largeAirports=largeA.to_crs(4326)
largeAirports.crs.axis_info
[Axis(name=Geodetic latitude, abbrev=Lat, direction=north, unit_auth_code=EPSG, unit_code=9122, unit_name=degree), Axis(name=Geodetic longitude, abbrev=Lon, direction=east, unit_auth_code=EPSG, unit_code=9122, unit_name=degree)]
# Ploteo
base=largeAirports.plot(color='red',marker="^")
seaports_jap_6684.plot(ax=base,alpha=0.5, markersize=3)
<Axes: >
seaports_jap_6684.head()
| portName | Country Code | Latitude | Longitude | geometry | |
|---|---|---|---|---|---|
| 0 | Mombetsu Ko | Japan | 44.350000 | 143.350000 | POINT (143.35 44.35) |
| 1 | Iwakuni Ko | Japan | 34.183333 | 132.250000 | POINT (132.25 34.183) |
| 2 | Hitachi | Japan | 36.500000 | 140.633333 | POINT (140.633 36.5) |
| 3 | Kushiro Ko | Japan | 42.983333 | 144.366667 | POINT (144.367 42.983) |
| 4 | Hanasaki Ko | Japan | 43.283333 | 145.583333 | POINT (145.583 43.283) |
largeAirports.head()
| name | kind | latitude_deg | longitude_deg | elevation_ft | iso_region | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 0 | Narita International Airport | large_airport | 35.764702 | 140.386002 | 141.0 | JP-12 | Narita | POINT (1487953.425 1208863.893) |
| 1 | Kansai International Airport | large_airport | 34.427299 | 135.244003 | 26.0 | JP-27 | Osaka | POINT (1035834.881 992037.816) |
| 2 | New Chitose Airport | large_airport | 42.775200 | 141.692001 | 82.0 | JP-01 | Sapporo | POINT (1449235.049 2015248.441) |
| 3 | Fukuoka Airport | large_airport | 33.585899 | 130.451004 | 32.0 | JP-40 | Fukuoka | POINT (599284.927 859521.885) |
| 4 | Kagoshima Airport | large_airport | 31.803400 | 130.718994 | 906.0 | JP-46 | Kagoshima | POINT (636838.567 662888.116) |
# distancia entre 'Mombetsu Ko' y 'Narita International Airportl' en km
largeAirports.iloc[0].geometry.distance(seaports_jap_6684.iloc[0].geometry)/1000
1916.982917535566
# Distancias entre aeropuertos y puertos
seaports_jap_6684.geometry.apply\
(lambda g: largeAirports.geometry.distance(g)/1000)
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1916.982918 | 1434.122723 | 2482.118909 | 1047.698556 | 919.098829 | 1580.202859 | 1473.005814 | 2101.240880 | 1856.072084 | 1832.986905 | 365.375780 | 378.854480 |
| 1 | 1916.997943 | 1434.137771 | 2482.133644 | 1047.713243 | 919.113851 | 1580.217911 | 1473.020856 | 2101.255919 | 1856.087113 | 1833.001949 | 365.387595 | 378.866695 |
| 2 | 1916.989976 | 1434.130115 | 2482.126869 | 1047.706549 | 919.106372 | 1580.210132 | 1473.013259 | 2101.248347 | 1856.079158 | 1832.994072 | 365.379065 | 378.858098 |
| 3 | 1916.982990 | 1434.122934 | 2482.119425 | 1047.699095 | 919.099111 | 1580.203018 | 1473.006049 | 2101.241126 | 1856.072163 | 1832.987021 | 365.374866 | 378.853630 |
| 4 | 1916.981857 | 1434.121848 | 2482.118472 | 1047.698153 | 919.098051 | 1580.201914 | 1473.004972 | 2101.240052 | 1856.071031 | 1832.985901 | 365.373631 | 378.852387 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 158 | 1916.992567 | 1434.132681 | 2482.129318 | 1047.708986 | 919.108920 | 1580.212709 | 1473.015820 | 2101.250905 | 1856.081748 | 1832.996657 | 365.381301 | 378.860391 |
| 159 | 1916.999123 | 1434.138895 | 2482.134616 | 1047.714202 | 919.114943 | 1580.219056 | 1473.021969 | 2101.257028 | 1856.088290 | 1833.003112 | 365.388927 | 378.868032 |
| 160 | 1916.994056 | 1434.134000 | 2482.130225 | 1047.709858 | 919.110149 | 1580.214094 | 1473.017108 | 2101.252181 | 1856.083230 | 1832.998095 | 365.383681 | 378.862733 |
| 161 | 1916.991494 | 1434.131654 | 2482.128421 | 1047.708100 | 919.107920 | 1580.211664 | 1473.014802 | 2101.249891 | 1856.080677 | 1832.995597 | 365.380118 | 378.859201 |
| 162 | 1916.984800 | 1434.124572 | 2482.120640 | 1047.700274 | 919.100657 | 1580.204722 | 1473.007656 | 2101.242719 | 1856.073965 | 1832.988779 | 365.377511 | 378.856244 |
163 rows × 12 columns
# Asignar los nombres de aeropuertos y puertos como índices
seaports_jap_6684.set_index('portName').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000)
| name | Narita International Airport | Kansai International Airport | New Chitose Airport | Fukuoka Airport | Kagoshima Airport | Chubu Centrair International Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base | Naha Airport / JASDF Naha Air Base | Kadena Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| portName | ||||||||||||
| Mombetsu Ko | 1916.982918 | 1434.122723 | 2482.118909 | 1047.698556 | 919.098829 | 1580.202859 | 1473.005814 | 2101.240880 | 1856.072084 | 1832.986905 | 365.375780 | 378.854480 |
| Iwakuni Ko | 1916.997943 | 1434.137771 | 2482.133644 | 1047.713243 | 919.113851 | 1580.217911 | 1473.020856 | 2101.255919 | 1856.087113 | 1833.001949 | 365.387595 | 378.866695 |
| Hitachi | 1916.989976 | 1434.130115 | 2482.126869 | 1047.706549 | 919.106372 | 1580.210132 | 1473.013259 | 2101.248347 | 1856.079158 | 1832.994072 | 365.379065 | 378.858098 |
| Kushiro Ko | 1916.982990 | 1434.122934 | 2482.119425 | 1047.699095 | 919.099111 | 1580.203018 | 1473.006049 | 2101.241126 | 1856.072163 | 1832.987021 | 365.374866 | 378.853630 |
| Hanasaki Ko | 1916.981857 | 1434.121848 | 2482.118472 | 1047.698153 | 919.098051 | 1580.201914 | 1473.004972 | 2101.240052 | 1856.071031 | 1832.985901 | 365.373631 | 378.852387 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Shimizu Ko | 1916.992567 | 1434.132681 | 2482.129318 | 1047.708986 | 919.108920 | 1580.212709 | 1473.015820 | 2101.250905 | 1856.081748 | 1832.996657 | 365.381301 | 378.860391 |
| Shimonoseki | 1916.999123 | 1434.138895 | 2482.134616 | 1047.714202 | 919.114943 | 1580.219056 | 1473.021969 | 2101.257028 | 1856.088290 | 1833.003112 | 365.388927 | 378.868032 |
| Tsuruga Ko | 1916.994056 | 1434.134000 | 2482.130225 | 1047.709858 | 919.110149 | 1580.214094 | 1473.017108 | 2101.252181 | 1856.083230 | 1832.998095 | 365.383681 | 378.862733 |
| Yokosuka Ko | 1916.991494 | 1434.131654 | 2482.128421 | 1047.708100 | 919.107920 | 1580.211664 | 1473.014802 | 2101.249891 | 1856.080677 | 1832.995597 | 365.380118 | 378.859201 |
| Wakkanai | 1916.984800 | 1434.124572 | 2482.120640 | 1047.700274 | 919.100657 | 1580.204722 | 1473.007656 | 2101.242719 | 1856.073965 | 1832.988779 | 365.377511 | 378.856244 |
163 rows × 12 columns
# Ordenar alfabeticamente filas y columnas
seaports_jap_6684.set_index('portName').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
| name | Chubu Centrair International Airport | Fukuoka Airport | Kadena Air Base | Kagoshima Airport | Kansai International Airport | Naha Airport / JASDF Naha Air Base | Narita International Airport | New Chitose Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| portName | ||||||||||||
| Abashiri Ko | 1580.202389 | 1047.698295 | 378.853592 | 919.098423 | 1434.122279 | 365.374874 | 1916.982403 | 2482.118635 | 1473.005383 | 2101.240455 | 1856.071573 | 1832.986413 |
| Aioi | 1580.215862 | 1047.711484 | 378.864424 | 919.111882 | 1434.135755 | 365.385341 | 1916.995845 | 2482.131863 | 1473.018856 | 2101.253926 | 1856.085017 | 1832.999874 |
| Akita-Funakawa Ko | 1580.208395 | 1047.704211 | 378.858327 | 919.104434 | 1434.128291 | 365.379441 | 1916.988387 | 2482.124566 | 1473.011396 | 2101.246468 | 1856.077558 | 1832.992409 |
| Amagasaki | 1580.215237 | 1047.711028 | 378.863523 | 919.111307 | 1434.135151 | 365.384433 | 1916.995186 | 2482.131395 | 1473.018262 | 2101.253336 | 1856.084361 | 1832.999232 |
| Aokata | 1580.221079 | 1047.716048 | 378.869997 | 919.116921 | 1434.140900 | 365.390858 | 1917.001171 | 2482.136477 | 1473.023966 | 2101.259020 | 1856.090336 | 1833.005148 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Yawatahama | 1580.218278 | 1047.713750 | 378.866616 | 919.114264 | 1434.138158 | 365.387483 | 1916.998276 | 2482.134142 | 1473.021252 | 2101.256319 | 1856.087448 | 1833.002299 |
| Yokkaichi | 1580.214130 | 1047.710094 | 378.862250 | 919.110249 | 1434.134064 | 365.383166 | 1916.994047 | 2482.130449 | 1473.017184 | 2101.252262 | 1856.083225 | 1832.998108 |
| Yokohama Ko | 1580.211615 | 1047.708011 | 378.859264 | 919.107857 | 1434.131599 | 365.380189 | 1916.991453 | 2482.128334 | 1473.014744 | 2101.249832 | 1856.080636 | 1832.995552 |
| Yokosuka Ko | 1580.211664 | 1047.708100 | 378.859201 | 919.107920 | 1434.131654 | 365.380118 | 1916.991494 | 2482.128421 | 1473.014802 | 2101.249891 | 1856.080677 | 1832.995597 |
| Yura | 1580.215950 | 1047.711805 | 378.863892 | 919.112045 | 1434.135874 | 365.384770 | 1916.995879 | 2482.132169 | 1473.018990 | 2101.254066 | 1856.085055 | 1832.999935 |
163 rows × 12 columns
distanceMatrixKM_sea_air= seaports_jap_6684.set_index('portName').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
# La distancia media desde un puerto marítimo hasta todos los grandes aeropuertos
distanceMatrixKM_sea_air.mean(axis=1).sort_values(ascending=True)
| 0 | |
|---|---|
| portName | |
| Nemuro Ko | 1448.978992 |
| Hanasaki Ko | 1448.979022 |
| Abashiri Ko | 1448.979559 |
| Mombetsu Ko | 1448.980061 |
| Kushiro Ko | 1448.980119 |
| ... | ... |
| Nakagusuku | 1449.002571 |
| Nishihara | 1449.002689 |
| Naha Ko | 1449.002777 |
| Hirara Ko | 1449.005437 |
| Ishigaki | 1449.006559 |
163 rows × 1 columns
# distancias promedio, mínimas y máximas de los puertos hacia los aeropuertos
SomeStats=pd.DataFrame()
SomeStats['mean']=distanceMatrixKM_sea_air.mean(axis=1)
SomeStats['min']=distanceMatrixKM_sea_air.min(axis=1)
SomeStats['max']=distanceMatrixKM_sea_air.max(axis=1)
SomeStats.head(10)
| mean | min | max | |
|---|---|---|---|
| portName | |||
| Abashiri Ko | 1448.979559 | 365.374874 | 2482.118635 |
| Aioi | 1448.992511 | 365.385341 | 2482.131863 |
| Akita-Funakawa Ko | 1448.985324 | 365.379441 | 2482.124566 |
| Amagasaki | 1448.991871 | 365.384433 | 2482.131395 |
| Aokata | 1448.997660 | 365.390858 | 2482.136477 |
| Aomori Ko | 1448.984137 | 365.378631 | 2482.123282 |
| Atsumi | 1448.990624 | 365.382756 | 2482.130439 |
| Beppu | 1448.995615 | 365.388390 | 2482.134776 |
| Chiba Ko | 1448.987813 | 365.379697 | 2482.127944 |
| Choshi | 1448.987133 | 365.378905 | 2482.127365 |
# aeropuerto más alejado de cada puerto marítimo
distanceMatrixKM_sea_air.idxmax(axis=1)
| 0 | |
|---|---|
| portName | |
| Abashiri Ko | New Chitose Airport |
| Aioi | New Chitose Airport |
| Akita-Funakawa Ko | New Chitose Airport |
| Amagasaki | New Chitose Airport |
| Aokata | New Chitose Airport |
| ... | ... |
| Yawatahama | New Chitose Airport |
| Yokkaichi | New Chitose Airport |
| Yokohama Ko | New Chitose Airport |
| Yokosuka Ko | New Chitose Airport |
| Yura | New Chitose Airport |
163 rows × 1 columns
# puerto marítimo más alejado de cada aeropuerto
distanceMatrixKM_sea_air.idxmax(axis=0)
| 0 | |
|---|---|
| name | |
| Chubu Centrair International Airport | Ishigaki |
| Fukuoka Airport | Ishigaki |
| Kadena Air Base | Ishigaki |
| Kagoshima Airport | Ishigaki |
| Kansai International Airport | Ishigaki |
| Naha Airport / JASDF Naha Air Base | Ishigaki |
| Narita International Airport | Ishigaki |
| New Chitose Airport | Ishigaki |
| Osaka International Airport | Ishigaki |
| Sendai Airport | Ishigaki |
| Tokyo Haneda International Airport | Ishigaki |
| Yokota Air Base | Ishigaki |
# aeropuerto más cercano a cada puerto marítimo
distanceMatrixKM_sea_air.idxmin(axis=1)
| 0 | |
|---|---|
| portName | |
| Abashiri Ko | Naha Airport / JASDF Naha Air Base |
| Aioi | Naha Airport / JASDF Naha Air Base |
| Akita-Funakawa Ko | Naha Airport / JASDF Naha Air Base |
| Amagasaki | Naha Airport / JASDF Naha Air Base |
| Aokata | Naha Airport / JASDF Naha Air Base |
| ... | ... |
| Yawatahama | Naha Airport / JASDF Naha Air Base |
| Yokkaichi | Naha Airport / JASDF Naha Air Base |
| Yokohama Ko | Naha Airport / JASDF Naha Air Base |
| Yokosuka Ko | Naha Airport / JASDF Naha Air Base |
| Yura | Naha Airport / JASDF Naha Air Base |
163 rows × 1 columns
# puerto marítimo más cercano a cada aeropuerto
distanceMatrixKM_sea_air.idxmin(axis=0)
| 0 | |
|---|---|
| name | |
| Chubu Centrair International Airport | Nemuro Ko |
| Fukuoka Airport | Nemuro Ko |
| Kadena Air Base | Nemuro Ko |
| Kagoshima Airport | Nemuro Ko |
| Kansai International Airport | Nemuro Ko |
| Naha Airport / JASDF Naha Air Base | Nemuro Ko |
| Narita International Airport | Nemuro Ko |
| New Chitose Airport | Nemuro Ko |
| Osaka International Airport | Nemuro Ko |
| Sendai Airport | Nemuro Ko |
| Tokyo Haneda International Airport | Nemuro Ko |
| Yokota Air Base | Nemuro Ko |
Ejercicio 2¶
- Utilice un mapa de puntos y un mapa de líneas de su país.
- Calcule la matriz de distancias para ambos.
- Seleccione una línea de la matriz de distancias y trace el punto más cercano y el más lejano a esa línea.
rivers.head()
| f_code | hyc | lit | nam | soc | geometry | |
|---|---|---|---|---|---|---|
| 0 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373410.491 44638.656, 373749.791 4... |
| 1 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373749.791 44838.884, 373376.037 4... |
| 2 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (379093.829 45402.433, 379850.108 4... |
| 3 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373749.791 44838.884, 375023.404 4... |
| 4 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (375437.64 45633.307, 375217.713 45... |
rivers
| f_code | hyc | lit | nam | soc | geometry | |
|---|---|---|---|---|---|---|
| 0 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373410.491 44638.656, 373749.791 4... |
| 1 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373749.791 44838.884, 373376.037 4... |
| 2 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (379093.829 45402.433, 379850.108 4... |
| 3 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (373749.791 44838.884, 375023.404 4... |
| 4 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (375437.64 45633.307, 375217.713 45... |
| ... | ... | ... | ... | ... | ... | ... |
| 4790 | BH140 | 8 | 2 | ONUMA | JPN | LINESTRING (1391412.015 2307235.768, 1391198.8... |
| 4791 | BH140 | 8 | 1 | KOETOI G. | JPN | LINESTRING (1390505.97 2308978.942, 1390526.22... |
| 4792 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (1396542.534 2313815.963, 1396963.1... |
| 4793 | BH140 | 8 | 1 | UNK | JPN | LINESTRING (1920917.686 2466259.985, 1921504.2... |
| 4794 | BH140 | 8 | 2 | UNK | JPN | LINESTRING (1165502.075 1200379.89, 1165902.75... |
4795 rows × 6 columns
print(len(rivers['nam'].unique()))
382
#Se entiende a los datos que contienen "UNK" como desconocidos
rivers_with_unk = rivers[rivers.nam.str.contains('UNK')]
print(rivers_with_unk['nam'].unique())
['UNK' 'UNK\r\n' 'UNK\r\n\r\n' 'UNK ISHIKARI GAWA TO TUNAGATTEIRUKAMO']
#Se filtrarán los ríos desconocidos ("UNK")
rivers = rivers[~rivers.nam.str.contains('UNK')]
#Se cambia "nam" a "name" para un mejor entendimiento
rivers = rivers.rename(columns={'nam': 'name'})
print(len(rivers['name'].unique()))
378
rivers.head()
| f_code | hyc | lit | name | soc | geometry | |
|---|---|---|---|---|---|---|
| 18 | BH140 | 8 | 1 | ANBO G. | JPN | LINESTRING (640341.959 494783.487, 640533.83 4... |
| 23 | BH140 | 8 | 1 | KIMOTSUKI G. | JPN | LINESTRING (668114.864 615221.236, 666979.228 ... |
| 25 | BH140 | 8 | 1 | KIMOTSUKI G. | JPN | LINESTRING (663537.594 615098.715, 661949.242 ... |
| 26 | BH140 | 8 | 1 | MANOSE G. | JPN | LINESTRING (608555.528 613449.112, 608760.762 ... |
| 27 | BH140 | 8 | 1 | MANOSE G. | JPN | LINESTRING (599901.222 620731.187, 600053.928 ... |
rivers['name'].unique()
array(['ANBO G.', 'KIMOTSUKI G.', 'MANOSE G.', 'HIROTO G.', 'HISHIDA G.',
'SHIN K.', 'SENDAI G.', 'OYODO G.', 'IWASE G.', 'HITOTSUSE G.',
'OMARU G.', 'KUMA G.', 'MIMI G.', 'KAWABE G.', 'ISUZU G.',
'MIDORI K.', 'KITA G.', 'SHIRA K.', 'KIKUCHI G.', 'BANJO G.',
'ONO G.', 'SHIMANTO G.', 'MATSUDA G.', 'YABE G.', 'CHIKUGO G.',
'OITA G.', 'KASE G.', 'KANOGAWA KO', 'HIJI G.', 'NIIDA G.',
'MATSUURA G.', 'YAMAGUNI G.', 'YUSUHARA G.', 'YAKKAN G.',
'HARAI K.', 'KOZA G.', 'NIYODO G.', 'NAHARI G.', 'ONGA G.',
'KAIFU G.', 'AKI G.', 'HIKI G.', 'MANOBE G.', 'KUMANO G.',
'YOSHINO G.', 'HYTkakunin', 'SHIGENOBU G.', 'NAKA G.',
'NAKAYAMA G.', 'DOZUN G.', 'KAMO G.', 'TONDA G.', 'HOUOU KO',
'IYA G.', 'KOTO G.', 'HIDAKA G.', 'KITAYAMA G.', 'ASA G.',
'KATSUURA G.', 'YOSHIDA G.', 'FUSHINO G.', 'TOTSU K.', 'ARIDA G.',
'SHIMATA G.', 'AKUI G.', 'ONO KO', 'SAITA G.', 'NISHIKI G.',
'SABA G.', 'KINO G.', 'OZE G.', 'DOKI G.', 'ABU G.', 'KUROSE G.',
'TAKATSU G.', 'AHIDA G.', 'NUTO G.', 'MIYA G.', 'ASHIDA G.',
'YAMATO G.', 'HIKIMI G.', 'ODA G.', 'ASAHI G.', 'KUMODE G.',
'IZUMO G.', 'KIKU G.', 'KUMOZUFURU K.', 'TENRYU G.', 'ENO K.',
'OTA G.', 'NABARI G.', 'HAMANA KO', 'YODO G.', 'INA G.', 'OI G.',
'TAKAHASHI G.', 'IGA G.', 'MISUMI G.', 'IBO G.', 'NARIWA G.',
'SUFU G.', 'SAIJO G.', 'MUKO G.', 'CHIKUSA G.', 'KIZU G.',
'UJI G.', 'HOZU G.', 'FUNAGIRA DAMUKO', 'SUZUKA G.', 'YAHAGI G.',
'ABE K.', 'ICHI K.', 'KONO G.', 'KANNOSE G.', 'GONO G.', 'KETA G.',
'KISO G.', 'KANDO G.', 'IBI G.', 'YUMESAKI G.', 'ETI G.',
'NIKKO G.', 'ECHI G.', 'HII K.', 'SHONAI G.', 'OCHISE G.',
'FUJI K.', 'KANSA G.', 'YASU G.', 'HINO G.', 'MIDORI KO',
'MACHIYA G.', 'TOKI G.', 'SESSO KO', 'SHIZUMA G.', 'MARUYAMA G.',
'IKAWA KO', 'YURA G.', 'SAKAWA G.', 'HAYA K.', 'ISUMI G.',
'KOITO G.', 'TENJIN G.', 'INASHI G.', 'ADO G.', 'FUJI G.\r\n',
'OBITSU G.', 'HAKUTA G.', 'SAGAMI G.', 'CYUKAI', 'SINJI KO',
'YATA G.', 'MINAMI G.', 'TSURUMI G.', 'TAMA G.', 'YORO G.',
'DASHI G.', 'FUEFUKI G.', 'SAKAI G.', 'KATSURA G.', 'KISHIDA G.',
'NEO G.', 'MURAYAMA G.', 'KAMANASHI G.', 'ASA K.', 'EDO G.',
'ARA K.', 'NAGARA G.', 'TAKENO G.', 'MASE G.', 'MASHITA G.',
'TOUSENKYO KANAYAMA KO', 'INBA NUMA', 'OTAKI G.', 'TONE G.',
'MIBU G.', 'MIURA TYOSUIIKE', 'FURUTONE G.', 'IRUMA G.',
'KITA URA', 'KINU G,', 'KASUMI-GA URA', 'KOMA G.', 'MOTO ARA K.',
'SUWA KO', 'ASUWA G.', 'CHIKUMA G.', 'NARAI G.', 'AZUSA KO',
'KOKAI G.', 'OMOI G,', 'OMOI G.', 'SAKURA G.', 'KUZURYU G.',
'AZUSA G.', 'WATARASE G.', 'TAISYO IKE', 'KABURA G.', 'KARASU G.',
'KINU G.', 'TAKAHARA G.', 'HIMUMA', 'DAIYOJI G.', 'HINUMA G.',
'SHIBAYAMA G.', 'TAKASE G.', 'KAKEHASHI G.', 'SAI G.', 'TEDORI G.',
'KUJI G.', 'AGATSUMA G.', 'SHO K.', 'JINZU G.', 'JOZANJI G.',
'SUSOBANA G.', 'JOUGANJI G.', 'KATASHINA G.', 'HIME K.', 'TA GAWA',
'CHUZENJI KO', 'DAIYA G.', 'HOKI G.', 'KIYOTSU G.', 'SAME K.',
'NAKATSU G.', 'SABI G.', 'NATSUI G.', 'SHINANO G.', 'SABAISHI G.',
'HOKURA G.', 'ABUKUMA G.', 'TADAMI G.', 'KIDO G.', 'INAWASHIRO KO',
'O KAWA', 'SHIBUMI G.', 'UKEDO G.', 'TOKONAMI G.',
'NAKA NO KUCHI G.', 'AGANO G.', 'HIBARA KO', 'HAYADE G.',
'MANO G.', 'HAYAMA KO', 'KAJI G.', 'TAINAI G.', 'SHIROSHI G.',
'SHIROISHI G.', 'MOGAMI G.', 'MATSU K.', 'ABUKUBA G.', 'NATORI G.',
'MIOMOTE G.', 'NANAKITA G.', 'HIROSE G.', 'SUKAWA G.',
'KITAKAMI G.', 'EAI G.', 'OGUNI G.', 'HASAMA G.', 'AKA G.',
'KESEN G.', 'MINASE G.', 'ISAWA G.', 'WAGA G.', 'KOYOSHI G.',
'SARUKAISHI G.', 'OMONO G.', 'HEI G.', 'OMOTO G.', 'BABAME G.',
'AKKA G.', 'ANI G.', 'YONESHIRO G.', 'UGE G.', 'MABECHI G.',
'NIIBA G.', 'OIRASE G.', 'ASASEISHI G.', 'GONOHE G.', 'AISAKA G.',
'ARA KAWA', 'SHITTSUNOHE G.', 'AKAISHI G.', 'NAKAMURA G.',
'OGAWARA KO', 'IWAKI G.', 'TONABU G.', 'SHIRIUCHI G.', 'ASSABU G.',
'HIDAKA HOROBETSU G.', 'YURAPPU G.', 'SHIZUNAI G.', 'MOTOURA K.',
'TOYONI G.', 'SHIRIBESHI TOSHIBETSU G.', 'REKIFUNE G.',
'APPETSU G.', 'OSARU G.', 'NIIKAPPU G.', 'SARU G.', 'MUKABIRA G.',
'SHIRAOI G.', 'TOKACHI G.', 'SHIKOTSU KO', 'YUFUTSU G.', 'MU KAWA',
'AZUMA G.', 'SATSUNAI G.', 'AZUMA G.\r\n\r\n', 'URAHORO G.',
'ABIRA G.', 'SHIRIBETSU G.', 'CHITOSE G.', 'ONBETSU G.',
'HORIKAPPU G.', 'SHIKARIBETSU G.', 'YUBARI G.', 'KUSHIRO G.',
'TOYOHIRA G.', 'AKAN G.', 'OTOFUKE G.', 'EBETSU G.',
'TOSHIBETSU G.', 'ISHIKARI G.', 'TOBETSU G.',
'BARATO G. ISHIKARI GAWA TO TUNAGATTEIRUKAMO', 'YOTCHI G.',
'BIRIBETSU G.', 'SAHORO G.', 'CHARO G.', 'ASHORO G.',
'BEKANBEUSHI G.', 'SHORO G.', 'SETSURI G.', 'FUREN G.', 'AKAN KO',
'TOKOTAN G.', 'NISHIBETSU G.', 'ASHIBETSU G.', 'SHUNBETSU G.',
'SORACHI G.', 'TOHORO G.', 'TOKORO G.', 'OKETO KO', 'SHIBETSU G.',
'CHUBETSU G.', 'KUSSHARO KO', 'CHURUI G.', 'MUKA G.',
'YANBETSU G.', 'SHARI G.', 'URYU G.', 'TOFUTSU KO', 'MOKOTO G.',
'RUMAI G.', 'ABASHIRI KO', 'URYUG.', 'YUBETSU G.',
'OBIRASHIBE G.', 'SAROMABETSU G.', 'KOTANBETSU G.',
'SHUMARINAI KO', 'NAYORO G.', 'HABORO G.', 'OKOPPE G.',
'MOKOPPE G.', 'ENBETSU G.', 'TESHIO G.', 'TOKUSHIBETSU G.',
'HOROBETSU G.', 'SAROBETSU G.', 'TONBETSU G.', 'RUBETSU G.',
'KUCCHARO KO', 'SARUFUTSU G.', 'KOETOI G.', 'ONUMA'], dtype=object)
#Buscamos uno de ellos
rivers[rivers.name.str.contains('TOHORO')]
| f_code | hyc | lit | name | soc | geometry | |
|---|---|---|---|---|---|---|
| 4456 | BH140 | 8 | 1 | TOHORO G. | JPN | LINESTRING (1712400.349 2176951.417, 1712115.3... |
#Distancia de cada aeropuerto a TOHORO
rivers[rivers.name.str.contains('TOHORO')].iloc[0].geometry.distance(largeAirports.set_index('name').geometry)/1000
| geometry | |
|---|---|
| name | |
| Narita International Airport | 964.139995 |
| Kansai International Airport | 1328.822323 |
| New Chitose Airport | 270.397139 |
| Fukuoka Airport | 1686.891758 |
| Kagoshima Airport | 1820.218243 |
| Chubu Centrair International Airport | 1207.474836 |
| Osaka International Airport | 1285.612683 |
| Sendai Airport | 696.895538 |
| Tokyo Haneda International Airport | 1007.961540 |
| Yokota Air Base | 1004.373970 |
| Naha Airport / JASDF Naha Air Base | 2501.407196 |
| Kadena Air Base | 2480.169216 |
distanceMatrixKM_riv_air=rivers.set_index('name').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_riv_air
| name | Chubu Centrair International Airport | Fukuoka Airport | Kadena Air Base | Kagoshima Airport | Kansai International Airport | Naha Airport / JASDF Naha Air Base | Narita International Airport | New Chitose Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | ||||||||||||
| ABASHIRI KO | 1217.283686 | 1677.182631 | 2480.229305 | 1816.532215 | 1333.154795 | 2501.435605 | 990.433074 | 244.551514 | 1289.520308 | 718.752026 | 1031.244435 | 1024.571465 |
| ABASHIRI KO | 1221.450033 | 1681.065622 | 2484.413919 | 1820.660498 | 1337.342462 | 2505.620110 | 994.375482 | 248.060198 | 1293.708457 | 722.761821 | 1035.246731 | 1028.624369 |
| ABASHIRI KO | 1221.883647 | 1681.065622 | 2484.483807 | 1820.660498 | 1337.587105 | 2505.688622 | 995.366575 | 248.060198 | 1293.941167 | 723.638463 | 1036.136728 | 1029.413953 |
| ABE K. | 145.207525 | 752.649206 | 1404.981922 | 802.095667 | 296.588420 | 1426.017681 | 208.081027 | 934.788240 | 272.380742 | 429.855373 | 146.885518 | 127.463080 |
| ABE K. | 147.570407 | 755.686219 | 1416.144168 | 808.983507 | 300.281871 | 1437.214887 | 197.428037 | 898.156506 | 273.393886 | 398.721079 | 137.065171 | 106.327763 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| YURA G. | 136.271003 | 489.826883 | 1232.644105 | 580.834985 | 93.659352 | 1253.887470 | 463.826545 | 1014.210373 | 52.156038 | 598.567185 | 405.740026 | 367.973183 |
| YURA G. | 146.116346 | 474.084438 | 1223.352566 | 568.810180 | 96.626687 | 1244.586170 | 471.604453 | 1015.365659 | 57.933202 | 602.928759 | 413.801842 | 375.619762 |
| YURA G. | 132.514027 | 498.133515 | 1237.317549 | 586.957292 | 95.367980 | 1258.564476 | 459.421105 | 1012.425724 | 53.114370 | 594.698752 | 401.353183 | 363.562105 |
| YURAPPU G. | 891.326203 | 1302.876721 | 2119.461831 | 1450.901618 | 988.902091 | 2140.558714 | 738.826818 | 131.905972 | 944.783387 | 472.158064 | 763.389468 | 743.754020 |
| YUSUHARA G. | 393.011867 | 232.048127 | 913.742596 | 263.398089 | 240.533547 | 934.955208 | 740.857837 | 1306.427070 | 275.752309 | 902.421690 | 679.818443 | 649.522678 |
1273 rows × 12 columns
#Aquí, vemos una fila (río, nombre exacto), que indica la distancia a cada columna (gran aeropuerto)
distanceMatrixKM_riv_air.loc['TOHORO G.'].sort_values()
| TOHORO G. | |
|---|---|
| name | |
| New Chitose Airport | 270.397139 |
| Sendai Airport | 696.895538 |
| Narita International Airport | 964.139995 |
| Yokota Air Base | 1004.373970 |
| Tokyo Haneda International Airport | 1007.961540 |
| Chubu Centrair International Airport | 1207.474836 |
| Osaka International Airport | 1285.612683 |
| Kansai International Airport | 1328.822323 |
| Fukuoka Airport | 1686.891758 |
| Kagoshima Airport | 1820.218243 |
| Kadena Air Base | 2480.169216 |
| Naha Airport / JASDF Naha Air Base | 2501.407196 |
!pip install folium matplotlib mapclassify
from mapclassify import classify
Requirement already satisfied: folium in /usr/local/lib/python3.10/dist-packages (0.18.0) Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.8.0) Collecting mapclassify Downloading mapclassify-2.8.1-py3-none-any.whl.metadata (2.8 kB) Requirement already satisfied: branca>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from folium) (0.8.0) Requirement already satisfied: jinja2>=2.9 in /usr/local/lib/python3.10/dist-packages (from folium) (3.1.4) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from folium) (1.26.4) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from folium) (2.32.3) Requirement already satisfied: xyzservices in /usr/local/lib/python3.10/dist-packages (from folium) (2024.9.0) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.3.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.55.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.7) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (24.2) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (11.0.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.2.0) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2) Requirement already satisfied: networkx>=2.7 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (3.4.2) Requirement already satisfied: pandas!=1.5.0,>=1.4 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (2.2.2) Requirement already satisfied: scikit-learn>=1.0 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.5.2) Requirement already satisfied: scipy>=1.8 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.13.1) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.9->folium) (3.0.2) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2024.2) Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2024.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0) Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0->mapclassify) (1.4.2) Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0->mapclassify) (3.5.0) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (3.4.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (2.2.3) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (2024.8.30) Downloading mapclassify-2.8.1-py3-none-any.whl (59 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 59.1/59.1 kB 2.3 MB/s eta 0:00:00 Installing collected packages: mapclassify Successfully installed mapclassify-2.8.1
base=largeAirports.explore(color='red',marker_kwds=dict(radius=10))
rivers[rivers.name.str.contains('TOHORO')].explore(m=base)
#Ahora, vamos a centrarnos en las varias polilíneas que se refieren a un mismo río.
rivers[~rivers.name.isna()]
#Esto sucede porque cada línea forma una parte de un mismo sistema de ríos
| f_code | hyc | lit | name | soc | geometry | |
|---|---|---|---|---|---|---|
| 18 | BH140 | 8 | 1 | ANBO G. | JPN | LINESTRING (640341.959 494783.487, 640533.83 4... |
| 23 | BH140 | 8 | 1 | KIMOTSUKI G. | JPN | LINESTRING (668114.864 615221.236, 666979.228 ... |
| 25 | BH140 | 8 | 1 | KIMOTSUKI G. | JPN | LINESTRING (663537.594 615098.715, 661949.242 ... |
| 26 | BH140 | 8 | 1 | MANOSE G. | JPN | LINESTRING (608555.528 613449.112, 608760.762 ... |
| 27 | BH140 | 8 | 1 | MANOSE G. | JPN | LINESTRING (599901.222 620731.187, 600053.928 ... |
| ... | ... | ... | ... | ... | ... | ... |
| 4779 | BH140 | 8 | 1 | SARUFUTSU G. | JPN | LINESTRING (1427986.228 2301873.03, 1428031.85... |
| 4786 | BH140 | 8 | 1 | SARUFUTSU G. | JPN | LINESTRING (1428701.64 2302851.219, 1429871.74... |
| 4787 | BH140 | 8 | 1 | KOETOI G. | JPN | LINESTRING (1392376.889 2306214.343, 1393127.7... |
| 4790 | BH140 | 8 | 2 | ONUMA | JPN | LINESTRING (1391412.015 2307235.768, 1391198.8... |
| 4791 | BH140 | 8 | 1 | KOETOI G. | JPN | LINESTRING (1390505.97 2308978.942, 1390526.22... |
1273 rows × 6 columns
#Disolvamos los que pertenecen a un mismo sistema de ríos en una multilínea:
name=rivers.dissolve(by='name')
name
| geometry | f_code | hyc | lit | soc | |
|---|---|---|---|---|---|
| name | |||||
| ABASHIRI KO | MULTILINESTRING ((1619681.843 2195475.388, 161... | BH140 | 8 | 2 | JPN |
| ABE K. | MULTILINESTRING ((1319703.289 1085929.375, 131... | BH140 | 8 | 1 | JPN |
| ABIRA G. | LINESTRING (1456120.306 2002642.709, 1456709.7... | BH140 | 8 | 1 | JPN |
| ABU G. | MULTILINESTRING ((696983.361 949047.64, 696948... | BH140 | 8 | 1 | JPN |
| ABUKUBA G. | LINESTRING (1480155.147 1474218.862, 1480677.6... | BH140 | 8 | 2 | JPN |
| ... | ... | ... | ... | ... | ... |
| YUFUTSU G. | LINESTRING (1428896.05 2007936.152, 1429050.3 ... | BH140 | 8 | 1 | JPN |
| YUMESAKI G. | LINESTRING (976663.994 1025342.223, 975670.044... | BH140 | 8 | 1 | JPN |
| YURA G. | MULTILINESTRING ((1040815.46 1085564.646, 1041... | BH140 | 8 | 1 | JPN |
| YURAPPU G. | LINESTRING (1333769.036 1936014.009, 1333840.8... | BH140 | 8 | 1 | JPN |
| YUSUHARA G. | LINESTRING (837832.473 833123.256, 838060.352 ... | BH140 | 8 | 1 | JPN |
378 rows × 5 columns
name.reset_index(drop=False,inplace=True)
name
| name | geometry | f_code | hyc | lit | soc | |
|---|---|---|---|---|---|---|
| 0 | ABASHIRI KO | MULTILINESTRING ((1619681.843 2195475.388, 161... | BH140 | 8 | 2 | JPN |
| 1 | ABE K. | MULTILINESTRING ((1319703.289 1085929.375, 131... | BH140 | 8 | 1 | JPN |
| 2 | ABIRA G. | LINESTRING (1456120.306 2002642.709, 1456709.7... | BH140 | 8 | 1 | JPN |
| 3 | ABU G. | MULTILINESTRING ((696983.361 949047.64, 696948... | BH140 | 8 | 1 | JPN |
| 4 | ABUKUBA G. | LINESTRING (1480155.147 1474218.862, 1480677.6... | BH140 | 8 | 2 | JPN |
| ... | ... | ... | ... | ... | ... | ... |
| 373 | YUFUTSU G. | LINESTRING (1428896.05 2007936.152, 1429050.3 ... | BH140 | 8 | 1 | JPN |
| 374 | YUMESAKI G. | LINESTRING (976663.994 1025342.223, 975670.044... | BH140 | 8 | 1 | JPN |
| 375 | YURA G. | MULTILINESTRING ((1040815.46 1085564.646, 1041... | BH140 | 8 | 1 | JPN |
| 376 | YURAPPU G. | LINESTRING (1333769.036 1936014.009, 1333840.8... | BH140 | 8 | 1 | JPN |
| 377 | YUSUHARA G. | LINESTRING (837832.473 833123.256, 838060.352 ... | BH140 | 8 | 1 | JPN |
378 rows × 6 columns
#Dado que en conjunto representan un sistema de ríos, se cambiará el nombre
name = name.rename(columns={'name': 'system'})
name
| system | geometry | f_code | hyc | lit | soc | |
|---|---|---|---|---|---|---|
| 0 | ABASHIRI KO | MULTILINESTRING ((1619681.843 2195475.388, 161... | BH140 | 8 | 2 | JPN |
| 1 | ABE K. | MULTILINESTRING ((1319703.289 1085929.375, 131... | BH140 | 8 | 1 | JPN |
| 2 | ABIRA G. | LINESTRING (1456120.306 2002642.709, 1456709.7... | BH140 | 8 | 1 | JPN |
| 3 | ABU G. | MULTILINESTRING ((696983.361 949047.64, 696948... | BH140 | 8 | 1 | JPN |
| 4 | ABUKUBA G. | LINESTRING (1480155.147 1474218.862, 1480677.6... | BH140 | 8 | 2 | JPN |
| ... | ... | ... | ... | ... | ... | ... |
| 373 | YUFUTSU G. | LINESTRING (1428896.05 2007936.152, 1429050.3 ... | BH140 | 8 | 1 | JPN |
| 374 | YUMESAKI G. | LINESTRING (976663.994 1025342.223, 975670.044... | BH140 | 8 | 1 | JPN |
| 375 | YURA G. | MULTILINESTRING ((1040815.46 1085564.646, 1041... | BH140 | 8 | 1 | JPN |
| 376 | YURAPPU G. | LINESTRING (1333769.036 1936014.009, 1333840.8... | BH140 | 8 | 1 | JPN |
| 377 | YUSUHARA G. | LINESTRING (837832.473 833123.256, 838060.352 ... | BH140 | 8 | 1 | JPN |
378 rows × 6 columns
#Otra matriz de distancias
distanceMatrixKM_sys_air=name.set_index('system').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_sys_air
| name | Chubu Centrair International Airport | Fukuoka Airport | Kadena Air Base | Kagoshima Airport | Kansai International Airport | Naha Airport / JASDF Naha Air Base | Narita International Airport | New Chitose Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| system | ||||||||||||
| ABASHIRI KO | 1217.283686 | 1677.182631 | 2480.229305 | 1816.532215 | 1333.154795 | 2501.435605 | 990.433074 | 244.551514 | 1289.520308 | 718.752026 | 1031.244435 | 1024.571465 |
| ABE K. | 144.983225 | 752.549728 | 1404.981922 | 802.095667 | 296.530054 | 1426.017681 | 197.428037 | 898.156506 | 272.126096 | 398.721079 | 137.065171 | 106.327763 |
| ABIRA G. | 986.107165 | 1428.597944 | 2236.137954 | 1570.402794 | 1094.514545 | 2257.309983 | 794.416866 | 9.515936 | 1050.567774 | 519.663076 | 827.203575 | 813.805987 |
| ABU G. | 471.180435 | 126.047354 | 958.063141 | 290.426400 | 324.278622 | 978.679111 | 813.569313 | 1285.798056 | 343.992731 | 934.729471 | 754.546203 | 718.136116 |
| ABUKUBA G. | 516.816790 | 1074.143714 | 1806.245385 | 1170.230932 | 655.682134 | 1827.452077 | 265.057045 | 538.129229 | 615.991719 | 5.783667 | 301.731212 | 295.988611 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| YUFUTSU G. | 983.783901 | 1416.725077 | 2227.784381 | 1560.932181 | 1089.287158 | 2248.933151 | 791.841251 | 9.936390 | 1045.255579 | 517.076335 | 824.669051 | 811.313853 |
| YUMESAKI G. | 196.752862 | 412.020326 | 1149.985786 | 496.844342 | 67.899759 | 1171.219711 | 533.547005 | 1066.577779 | 71.687692 | 665.207353 | 475.023172 | 437.853553 |
| YURA G. | 132.514027 | 473.345685 | 1223.352566 | 568.673177 | 93.659352 | 1244.586170 | 459.421105 | 995.587692 | 52.156038 | 591.629144 | 401.353183 | 363.562105 |
| YURAPPU G. | 891.326203 | 1302.876721 | 2119.461831 | 1450.901618 | 988.902091 | 2140.558714 | 738.826818 | 131.905972 | 944.783387 | 472.158064 | 763.389468 | 743.754020 |
| YUSUHARA G. | 393.011867 | 232.048127 | 913.742596 | 263.398089 | 240.533547 | 934.955208 | 740.857837 | 1306.427070 | 275.752309 | 902.421690 | 679.818443 | 649.522678 |
378 rows × 12 columns
#Se obtienen todas las distancias mínimas
mins=distanceMatrixKM_sys_air.idxmin(axis="columns") # same as axis=1
mins
| 0 | |
|---|---|
| system | |
| ABASHIRI KO | New Chitose Airport |
| ABE K. | Yokota Air Base |
| ABIRA G. | New Chitose Airport |
| ABU G. | Fukuoka Airport |
| ABUKUBA G. | Sendai Airport |
| ... | ... |
| YUFUTSU G. | New Chitose Airport |
| YUMESAKI G. | Kansai International Airport |
| YURA G. | Osaka International Airport |
| YURAPPU G. | New Chitose Airport |
| YUSUHARA G. | Fukuoka Airport |
378 rows × 1 columns
#Se elige uno de ellos
mins.iloc[1]
'Yokota Air Base'
base=name.explore()
# the closest
largeAirports[largeAirports.name.isin(mins)].explore(m=base,color='red',marker_kwds=dict(radius=10))
# NOT the closest
largeAirports[~largeAirports.name.isin(mins)].explore(m=base,color='blue',marker_kwds=dict(radius=5))
name.convex_hull
| 0 | |
|---|---|
| 0 | POLYGON ((1619821.192 2190479.177, 1619655.376... |
| 1 | POLYGON ((1319703.289 1085929.375, 1319291.634... |
| 2 | POLYGON ((1456120.306 2002642.709, 1459254.619... |
| 3 | POLYGON ((699425.217 946543.319, 698896.171 94... |
| 4 | POLYGON ((1489593.48 1473915.864, 1480155.147 ... |
| ... | ... |
| 373 | POLYGON ((1456782.408 2000091.379, 1456330.922... |
| 374 | POLYGON ((976663.994 1025342.223, 975670.044 1... |
| 375 | POLYGON ((1040815.46 1085564.646, 1014604.814 ... |
| 376 | POLYGON ((1342483.195 1933283.827, 1339369.542... |
| 377 | POLYGON ((837832.473 833123.256, 831300.539 85... |
378 rows × 1 columns
name.convex_hull.plot()
<Axes: >
name_hulls=name.convex_hull.to_frame()
name_hulls['name']=name['system'].tolist()
name_hulls.rename(columns={0:'geometry'},inplace=True)
name_hulls=name_hulls.set_geometry('geometry')
name_hulls.crs="EPSG:6684"
name_hulls
| geometry | name | |
|---|---|---|
| 0 | POLYGON ((1619821.192 2190479.177, 1619655.376... | ABASHIRI KO |
| 1 | POLYGON ((1319703.289 1085929.375, 1319291.634... | ABE K. |
| 2 | POLYGON ((1456120.306 2002642.709, 1459254.619... | ABIRA G. |
| 3 | POLYGON ((699425.217 946543.319, 698896.171 94... | ABU G. |
| 4 | POLYGON ((1489593.48 1473915.864, 1480155.147 ... | ABUKUBA G. |
| ... | ... | ... |
| 373 | POLYGON ((1456782.408 2000091.379, 1456330.922... | YUFUTSU G. |
| 374 | POLYGON ((976663.994 1025342.223, 975670.044 1... | YUMESAKI G. |
| 375 | POLYGON ((1040815.46 1085564.646, 1014604.814 ... | YURA G. |
| 376 | POLYGON ((1342483.195 1933283.827, 1339369.542... | YURAPPU G. |
| 377 | POLYGON ((837832.473 833123.256, 831300.539 85... | YUSUHARA G. |
378 rows × 2 columns
#La matriz de distancias
distanceMatrixKM_sysHull_air=name_hulls.set_index('name').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_sysHull_air
| name | Chubu Centrair International Airport | Fukuoka Airport | Kadena Air Base | Kagoshima Airport | Kansai International Airport | Naha Airport / JASDF Naha Air Base | Narita International Airport | New Chitose Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | ||||||||||||
| ABASHIRI KO | 1217.283686 | 1677.182631 | 2480.229305 | 1816.532215 | 1333.154795 | 2501.435605 | 990.433074 | 244.551514 | 1289.520308 | 718.752026 | 1031.244435 | 1024.571465 |
| ABE K. | 144.983225 | 752.549728 | 1404.981922 | 802.095667 | 296.530054 | 1426.017681 | 197.428037 | 898.156506 | 272.069251 | 398.721079 | 137.065171 | 106.327763 |
| ABIRA G. | 986.107165 | 1428.597944 | 2236.137954 | 1570.402794 | 1094.514545 | 2257.309983 | 794.416866 | 8.779286 | 1050.567774 | 519.663076 | 827.203575 | 813.805987 |
| ABU G. | 471.180435 | 126.047354 | 958.063141 | 290.426400 | 324.278622 | 978.679111 | 813.569313 | 1285.798056 | 343.992731 | 934.729471 | 754.546203 | 718.136116 |
| ABUKUBA G. | 516.816790 | 1074.143714 | 1806.245385 | 1170.230932 | 655.682134 | 1827.452077 | 265.057045 | 538.129229 | 615.991719 | 5.783667 | 301.731212 | 295.988611 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| YUFUTSU G. | 983.680813 | 1416.725077 | 2227.784381 | 1560.932181 | 1089.287158 | 2248.933151 | 791.841251 | 9.527550 | 1045.255579 | 517.076335 | 824.669051 | 811.313853 |
| YUMESAKI G. | 196.752862 | 412.020326 | 1149.985786 | 496.844342 | 67.899759 | 1171.219711 | 533.547005 | 1066.577779 | 71.677993 | 665.207353 | 475.023172 | 437.853553 |
| YURA G. | 132.514027 | 473.345685 | 1223.352566 | 568.673177 | 93.659352 | 1244.586170 | 459.421105 | 995.587692 | 52.156038 | 591.629144 | 401.353183 | 363.562105 |
| YURAPPU G. | 891.326203 | 1302.876721 | 2119.461831 | 1450.901618 | 988.902091 | 2140.558714 | 738.826818 | 131.905972 | 944.783387 | 472.158064 | 763.389468 | 743.754020 |
| YUSUHARA G. | 393.011867 | 232.047915 | 913.742596 | 263.398089 | 240.533547 | 934.955208 | 740.857837 | 1306.427070 | 275.752309 | 902.421690 | 679.818443 | 649.522678 |
378 rows × 12 columns
mins=distanceMatrixKM_sysHull_air.idxmin(axis="columns")
mins
| 0 | |
|---|---|
| name | |
| ABASHIRI KO | New Chitose Airport |
| ABE K. | Yokota Air Base |
| ABIRA G. | New Chitose Airport |
| ABU G. | Fukuoka Airport |
| ABUKUBA G. | Sendai Airport |
| ... | ... |
| YUFUTSU G. | New Chitose Airport |
| YUMESAKI G. | Kansai International Airport |
| YURA G. | Osaka International Airport |
| YURAPPU G. | New Chitose Airport |
| YUSUHARA G. | Fukuoka Airport |
378 rows × 1 columns
base=name_hulls.explore()
largeAirports[largeAirports.name.isin(mins)].explore(m=base,color='red',marker_kwds=dict(radius=10))
largeAirports[~largeAirports.name.isin(mins)].explore(m=base,color='blue',marker_kwds=dict(radius=5))
#Se usará el sistema disuelto
distanceMatrixKM_sys_air
| name | Chubu Centrair International Airport | Fukuoka Airport | Kadena Air Base | Kagoshima Airport | Kansai International Airport | Naha Airport / JASDF Naha Air Base | Narita International Airport | New Chitose Airport | Osaka International Airport | Sendai Airport | Tokyo Haneda International Airport | Yokota Air Base |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| system | ||||||||||||
| ABASHIRI KO | 1217.283686 | 1677.182631 | 2480.229305 | 1816.532215 | 1333.154795 | 2501.435605 | 990.433074 | 244.551514 | 1289.520308 | 718.752026 | 1031.244435 | 1024.571465 |
| ABE K. | 144.983225 | 752.549728 | 1404.981922 | 802.095667 | 296.530054 | 1426.017681 | 197.428037 | 898.156506 | 272.126096 | 398.721079 | 137.065171 | 106.327763 |
| ABIRA G. | 986.107165 | 1428.597944 | 2236.137954 | 1570.402794 | 1094.514545 | 2257.309983 | 794.416866 | 9.515936 | 1050.567774 | 519.663076 | 827.203575 | 813.805987 |
| ABU G. | 471.180435 | 126.047354 | 958.063141 | 290.426400 | 324.278622 | 978.679111 | 813.569313 | 1285.798056 | 343.992731 | 934.729471 | 754.546203 | 718.136116 |
| ABUKUBA G. | 516.816790 | 1074.143714 | 1806.245385 | 1170.230932 | 655.682134 | 1827.452077 | 265.057045 | 538.129229 | 615.991719 | 5.783667 | 301.731212 | 295.988611 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| YUFUTSU G. | 983.783901 | 1416.725077 | 2227.784381 | 1560.932181 | 1089.287158 | 2248.933151 | 791.841251 | 9.936390 | 1045.255579 | 517.076335 | 824.669051 | 811.313853 |
| YUMESAKI G. | 196.752862 | 412.020326 | 1149.985786 | 496.844342 | 67.899759 | 1171.219711 | 533.547005 | 1066.577779 | 71.687692 | 665.207353 | 475.023172 | 437.853553 |
| YURA G. | 132.514027 | 473.345685 | 1223.352566 | 568.673177 | 93.659352 | 1244.586170 | 459.421105 | 995.587692 | 52.156038 | 591.629144 | 401.353183 | 363.562105 |
| YURAPPU G. | 891.326203 | 1302.876721 | 2119.461831 | 1450.901618 | 988.902091 | 2140.558714 | 738.826818 | 131.905972 | 944.783387 | 472.158064 | 763.389468 | 743.754020 |
| YUSUHARA G. | 393.011867 | 232.048127 | 913.742596 | 263.398089 | 240.533547 | 934.955208 | 740.857837 | 1306.427070 | 275.752309 | 902.421690 | 679.818443 | 649.522678 |
378 rows × 12 columns
# Escogiendo un rio , valor mínimo
distanceMatrixKM_sys_air.loc['ARA K.'].min()
11.965887088929707
minMts=distanceMatrixKM_sys_air.loc['ARA K.'].min()*50 # km
# Buffer del rio
rivers[rivers.name=='ARA K.'].buffer(distance = minMts)
| 0 | |
|---|---|
| 1864 | POLYGON ((1439908.441 1195134.728, 1439919.852... |
| 1869 | POLYGON ((1439498.477 1195512.157, 1439539.668... |
| 1882 | POLYGON ((1436961.577 1197909.268, 1438127.45 ... |
| 1918 | POLYGON ((1429488.988 1200446.419, 1429819.741... |
| 2036 | POLYGON ((1410331.337 1211455.729, 1410387.052... |
| 2118 | POLYGON ((1402890.236 1221662.765, 1403827.204... |
| 2159 | POLYGON ((1401719.121 1222743.958, 1401690.837... |
| 2235 | POLYGON ((1386093.467 1235060.938, 1386509.885... |
| 2236 | POLYGON ((1363369.923 1224412.622, 1363390.125... |
| 2237 | POLYGON ((1383360.896 1234649.125, 1383404.249... |
| 3350 | POLYGON ((1363213.181 1457258.844, 1363146.259... |
# Visualización
bufferAroundARA=rivers[rivers.name=='ARA K.'].buffer(distance = minMts)
bufferAsBase=bufferAroundARA.explore(color='red')
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':1})
# Importando los aeropuertos pequeños
small_airports=airports[airports['kind']=='small_airport']
# plotting
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':0.5})
small_airports.explore(m=bufferAsBase,color='black')
# Mantener los aeropuertos dentro del buffer
riversWithinBuffer=small_airports.clip(mask=bufferAroundARA)
riversWithinBuffer
| name | kind | latitude_deg | longitude_deg | elevation_ft | iso_region | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 403 | Kumagaya Flying Club Osato Airfield | small_airport | 36.112460 | 139.413100 | NaN | JP-11 | Kumagaya | POINT (1392627.42 1233045.482) |
| 3029 | Kumagaya Trike Club Airfield | small_airport | 36.121580 | 139.400290 | 86.0 | JP-11 | Kumagaya | POINT (1391295.745 1233878.823) |
| 320 | Honda Airport | small_airport | 35.976330 | 139.524125 | NaN | JP-11 | Kawajima | POINT (1405226.731 1219424.722) |
| 395 | Mint Airstrip | small_airport | 36.002160 | 139.498000 | NaN | JP-11 | Kawajima | POINT (1402372.569 1221933.565) |
| 397 | Yuhikoku Kawajima Airstrip | small_airport | 36.011448 | 139.500289 | NaN | JP-11 | Kawajima | POINT (1402411.305 1223009.37) |
| 394 | Tokyo Flying Club Airstrip | small_airport | 36.035460 | 139.500880 | NaN | JP-11 | Konosu | POINT (1402025.772 1225711.421) |
| 401 | Air Wing Motor Paraglider School Osato Landing... | small_airport | 36.087990 | 139.431460 | NaN | JP-11 | Kumagaya | POINT (1394744.455 1230573.005) |
| 361 | Iwabuchi Watergate Auxiliary Airfield | small_airport | 35.782031 | 139.738133 | NaN | JP-13 | Kita, Tokyo | POINT (1428344.169 1200829.351) |
# ploteo de aeropuertos dentro del buffer
bufferAsBase=bufferAroundARA.explore(color='red')
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':1})
riversWithinBuffer.explore(m=bufferAsBase,color='black')
# Mínimo de todos los mínimos por fila
distanceMatrixKM_sys_air.min(axis=1).min()
0.7952852405805073
# usando el valor anterior
minMinMts_5=5*distanceMatrixKM_sys_air.min(axis=1).min()*500
allMinBuffer=rivers.buffer(distance = minMinMts_5).explore(color='red')
rivers.explore(m=allMinBuffer,color='blue',style_kwds={'weight':0.75})
# Buffer de todos los ríos
riversAll_buf=rivers.buffer(distance = minMinMts_5)
riversAll_buf
| 0 | |
|---|---|
| 18 | POLYGON ((633554.485 497789.938, 633481.88 497... |
| 23 | POLYGON ((665455.602 616562.567, 666384.034 61... |
| 25 | POLYGON ((661946.206 613112.93, 661751.342 613... |
| 26 | POLYGON ((608598.171 611463.601, 608476.211 61... |
| 27 | POLYGON ((599133.659 618899.535, 599066.47 618... |
| ... | ... |
| 4779 | POLYGON ((1430008.535 2301665.045, 1430020.051... |
| 4786 | POLYGON ((1429256.295 2305122.691, 1429444.566... |
| 4787 | POLYGON ((1392111.274 2304170.183, 1391609.272... |
| 4790 | POLYGON ((1389739.838 2308307.381, 1389774.041... |
| 4791 | POLYGON ((1389427.709 2306943.308, 1389401.863... |
1273 rows × 1 columns
# información
allRiversWithinBuffs=small_airports.clip(riversAll_buf)
allRiversWithinBuffs
| name | kind | latitude_deg | longitude_deg | elevation_ft | iso_region | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 1303 | Taragi Auxiliary Airfield | small_airport | 32.276669 | 130.915607 | NaN | JP-43 | Taragi | POINT (652126.25 716707.08) |
| 467 | Shirakawa Gliderport | small_airport | 32.776029 | 130.630532 | NaN | JP-43 | Kumamoto | POINT (621720.608 770527.433) |
| 3508 | Ozuki Airfield | small_airport | 34.045300 | 131.052002 | 13.0 | JP-35 | Shimonoseki | POINT (651703.34 914283.914) |
| 469 | Hofu Gliderport | small_airport | 34.039370 | 131.540310 | NaN | JP-35 | Hofu | POINT (696972.88 916865.687) |
| 855 | Ryu's Skypark | small_airport | 34.638053 | 136.545568 | NaN | JP-24 | Tsu | POINT (1153412.048 1029972.088) |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1653 | Yausubetsu Airfield | small_airport | 43.287200 | 144.997800 | 108.0 | JP-01 | Betsukai | POINT (1705595.702 2136981.909) |
| 466 | Betsukai Flight Park | small_airport | 43.474840 | 144.783090 | 407.0 | JP-01 | Betsukai | POINT (1682695.261 2153291.301) |
| 460 | Kitami Agricultural Airfield / Skyport Kitami | small_airport | 43.779980 | 143.730640 | 609.0 | JP-01 | Kitami | POINT (1589025.95 2165571.973) |
| 1660 | Otaki Airfield | small_airport | 42.665802 | 141.101933 | 1621.0 | JP-01 | Date | POINT (1403367.264 1992761.454) |
| 463 | Takikawa Sky Park | small_airport | 43.549349 | 141.894103 | 75.0 | JP-01 | Takikawa | POINT (1446781.404 2105034.171) |
65 rows × 8 columns
# Ploteo
base=riversAll_buf.plot(color='yellow')
allRiversWithinBuffs.plot(ax=base, color='green', markersize=1)
<Axes: >
# Visualización
base=riversAll_buf.explore(color='yellow')
allRiversWithinBuffs.explore(m=base, color='green')
Ejercicio 5¶
Obtenga un mapa de polígonos de la unidad administrativa más baja posible.
Obtenga una tabla de variables para esas unidades. Al menos 3 variables numéricas.
Preprocese ambas tablas y prepárelas para la fusión.
Realice la fusión, haciendo los cambios necesarios para conservar la mayor cantidad de columnas.
# Se carga el mapa de polígonos (la unidad administrativa más baja disponible)
units_admin = japon_states
# Se inspecciona las primeras filas
units_admin.head()
| ADM0_EN | ADM0_JA | ADM0_PCODE | ADM1_EN | ADM1_JA | ADM1_PCODE | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | Japan | 日本 | JP | Aichi | 愛知県 | JP23 | MULTIPOLYGON (((1195512.712 1039956.796, 11953... |
| 1 | Japan | 日本 | JP | Akita | 秋田県 | JP05 | MULTIPOLYGON (((1376309.983 1576859.486, 13757... |
| 2 | Japan | 日本 | JP | Aomori | 青森県 | JP02 | MULTIPOLYGON (((1488100.706 1763108.389, 14877... |
| 3 | Japan | 日本 | JP | Chiba | 千葉県 | JP12 | MULTIPOLYGON (((1466610.222 1182881.245, 14660... |
| 4 | Japan | 日本 | JP | Ehime | 愛媛県 | JP38 | MULTIPOLYGON (((801590.554 801225.044, 802058.... |
# Graficamos para verificar
units_admin.plot()
<Axes: >
units_admin.info
pandas.core.frame.DataFrame.info
def info(verbose: bool | None=None, buf: WriteBuffer[str] | None=None, max_cols: int | None=None, memory_usage: bool | str | None=None, show_counts: bool | None=None) -> None
Print a concise summary of a DataFrame. This method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage. Parameters ---------- verbose : bool, optional Whether to print the full summary. By default, the setting in ``pandas.options.display.max_info_columns`` is followed. buf : writable buffer, defaults to sys.stdout Where to send the output. By default, the output is printed to sys.stdout. Pass a writable buffer if you need to further process the output. max_cols : int, optional When to switch from the verbose to the truncated output. If the DataFrame has more than `max_cols` columns, the truncated output is used. By default, the setting in ``pandas.options.display.max_info_columns`` is used. memory_usage : bool, str, optional Specifies whether total memory usage of the DataFrame elements (including the index) should be displayed. By default, this follows the ``pandas.options.display.memory_usage`` setting. True always show memory usage. False never shows memory usage. A value of 'deep' is equivalent to "True with deep introspection". Memory usage is shown in human-readable units (base-2 representation). Without deep introspection a memory estimation is made based in column dtype and number of rows assuming values consume the same memory amount for corresponding dtypes. With deep memory introspection, a real memory usage calculation is performed at the cost of computational resources. See the :ref:`Frequently Asked Questions <df-memory-usage>` for more details. show_counts : bool, optional Whether to show the non-null counts. By default, this is shown only if the DataFrame is smaller than ``pandas.options.display.max_info_rows`` and ``pandas.options.display.max_info_columns``. A value of True always shows the counts, and False never shows the counts. Returns ------- None This method prints a summary of a DataFrame and returns None. See Also -------- DataFrame.describe: Generate descriptive statistics of DataFrame columns. DataFrame.memory_usage: Memory usage of DataFrame columns. Examples -------- >>> int_values = [1, 2, 3, 4, 5] >>> text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon'] >>> float_values = [0.0, 0.25, 0.5, 0.75, 1.0] >>> df = pd.DataFrame({"int_col": int_values, "text_col": text_values, ... "float_col": float_values}) >>> df int_col text_col float_col 0 1 alpha 0.00 1 2 beta 0.25 2 3 gamma 0.50 3 4 delta 0.75 4 5 epsilon 1.00 Prints information of all columns: >>> df.info(verbose=True) <class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 int_col 5 non-null int64 1 text_col 5 non-null object 2 float_col 5 non-null float64 dtypes: float64(1), int64(1), object(1) memory usage: 248.0+ bytes Prints a summary of columns count and its dtypes but not per column information: >>> df.info(verbose=False) <class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Columns: 3 entries, int_col to float_col dtypes: float64(1), int64(1), object(1) memory usage: 248.0+ bytes Pipe output of DataFrame.info to buffer instead of sys.stdout, get buffer content and writes to a text file: >>> import io >>> buffer = io.StringIO() >>> df.info(buf=buffer) >>> s = buffer.getvalue() >>> with open("df_info.txt", "w", ... encoding="utf-8") as f: # doctest: +SKIP ... f.write(s) 260 The `memory_usage` parameter allows deep introspection mode, specially useful for big DataFrames and fine-tune memory optimization: >>> random_strings_array = np.random.choice(['a', 'b', 'c'], 10 ** 6) >>> df = pd.DataFrame({ ... 'column_1': np.random.choice(['a', 'b', 'c'], 10 ** 6), ... 'column_2': np.random.choice(['a', 'b', 'c'], 10 ** 6), ... 'column_3': np.random.choice(['a', 'b', 'c'], 10 ** 6) ... }) >>> df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 column_1 1000000 non-null object 1 column_2 1000000 non-null object 2 column_3 1000000 non-null object dtypes: object(3) memory usage: 22.9+ MB >>> df.info(memory_usage='deep') <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 column_1 1000000 non-null object 1 column_2 1000000 non-null object 2 column_3 1000000 non-null object dtypes: object(3) memory usage: 165.9 MB
import random
import pandas as pd
import geopandas as gpd
# Se crea una tabla de variables asociada a las unidades administrativas
# Las unidades administrativas tienen una columna "name" para poder asociar las variables.
variables_table = pd.DataFrame({
"name": units_admin["ADM1_EN"], # Tomar el nombre de la unidad administrativa
"ADM1_JA": units_admin["ADM1_JA"], # Copiar la columna ADM1_JA de units_admin
"population": [random.randint(1000, 5000) for _ in range(len(units_admin))], # Ejemplo: población
"area_km2": [random.uniform(10, 100) for _ in range(len(units_admin))], # Ejemplo: área
"density": [random.uniform(50, 300) for _ in range(len(units_admin))] # Ejemplo: densidad
})
print(variables_table.head()) #Se observan las variables
name ADM1_JA population area_km2 density 0 Aichi 愛知県 2407 75.351161 211.308909 1 Akita 秋田県 4310 64.664937 231.813474 2 Aomori 青森県 2091 52.093732 70.515923 3 Chiba 千葉県 3742 31.186209 136.697740 4 Ehime 愛媛県 1873 47.311352 101.132225
# Preprocesar las tablas
# Se asegura que ambas tablas tienen el mismo tipo en las columnas clave
units_admin["ADM1_JA"] = units_admin["ADM1_JA"].astype(str)
variables_table["ADM1_JA"] = variables_table["ADM1_JA"].astype(str)
units_admin.head()
| ADM0_EN | ADM0_JA | ADM0_PCODE | ADM1_EN | ADM1_JA | ADM1_PCODE | geometry | name | |
|---|---|---|---|---|---|---|---|---|
| 0 | Japan | 日本 | JP | Aichi | 愛知県 | JP23 | MULTIPOLYGON (((1195512.712 1039956.796, 11953... | 愛知県 |
| 1 | Japan | 日本 | JP | Akita | 秋田県 | JP05 | MULTIPOLYGON (((1376309.983 1576859.486, 13757... | 秋田県 |
| 2 | Japan | 日本 | JP | Aomori | 青森県 | JP02 | MULTIPOLYGON (((1488100.706 1763108.389, 14877... | 青森県 |
| 3 | Japan | 日本 | JP | Chiba | 千葉県 | JP12 | MULTIPOLYGON (((1466610.222 1182881.245, 14660... | 千葉県 |
| 4 | Japan | 日本 | JP | Ehime | 愛媛県 | JP38 | MULTIPOLYGON (((801590.554 801225.044, 802058.... | 愛媛県 |
# Asegurar que las columnas clave tienen el mismo tipo
units_admin["ADM1_JA"] = units_admin["ADM1_JA"].astype(str)
variables_table["ADM1_JA"] = variables_table["ADM1_JA"].astype(str)
# Se fucionan usando ADM1_JA
fusion_data = units_admin.merge(variables_table, on="ADM1_JA", how="left")
# Se verifica el resultado
print(fusion_data.head())
ADM0_EN ADM0_JA ADM0_PCODE ADM1_EN ADM1_JA ADM1_PCODE \
0 Japan 日本 JP Aichi 愛知県 JP23
1 Japan 日本 JP Akita 秋田県 JP05
2 Japan 日本 JP Aomori 青森県 JP02
3 Japan 日本 JP Chiba 千葉県 JP12
4 Japan 日本 JP Ehime 愛媛県 JP38
geometry name_x name_y \
0 MULTIPOLYGON (((1195512.712 1039956.796, 11953... 愛知県 aichi
1 MULTIPOLYGON (((1376309.983 1576859.486, 13757... 秋田県 akita
2 MULTIPOLYGON (((1488100.706 1763108.389, 14877... 青森県 aomori
3 MULTIPOLYGON (((1466610.222 1182881.245, 14660... 千葉県 chiba
4 MULTIPOLYGON (((801590.554 801225.044, 802058.... 愛媛県 ehime
population area_km2 density
0 2407 75.351161 211.308909
1 4310 64.664937 231.813474
2 2091 52.093732 70.515923
3 3742 31.186209 136.697740
4 1873 47.311352 101.132225
# Finalmente, se guarda el resultado como un nuevo archivo GeoPackage
fusion_data.to_file("unidad_admin_fusionada.gpkg", driver="GPKG")
# Se visualiza el resultado
fusion_data.plot(column="area_km2", legend=True)
<Axes: >
Ejercicio 7¶
Calcule el coeficiente de Moran para una de las tres variables numéricas.
Haga un diagrama de dispersión para cada variable.
# Instalar las librerías necesarias
!pip install libpysal esda splot
import geopandas as gpd
from libpysal.weights import KNN
from esda.moran import Moran
from splot.esda import moran_scatterplot
import matplotlib.pyplot as plt
Collecting libpysal Downloading libpysal-4.12.1-py3-none-any.whl.metadata (4.8 kB) Collecting esda Downloading esda-2.6.0-py3-none-any.whl.metadata (2.0 kB) Collecting splot Downloading splot-1.1.7-py3-none-any.whl.metadata (8.9 kB) Requirement already satisfied: beautifulsoup4>=4.10 in /usr/local/lib/python3.10/dist-packages (from libpysal) (4.12.3) Requirement already satisfied: geopandas>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from libpysal) (1.0.1) Requirement already satisfied: numpy>=1.22 in /usr/local/lib/python3.10/dist-packages (from libpysal) (1.26.4) Requirement already satisfied: packaging>=22 in /usr/local/lib/python3.10/dist-packages (from libpysal) (24.2) Requirement already satisfied: pandas>=1.4 in /usr/local/lib/python3.10/dist-packages (from libpysal) (2.2.2) Requirement already satisfied: platformdirs>=2.0.2 in /usr/local/lib/python3.10/dist-packages (from libpysal) (4.3.6) Requirement already satisfied: requests>=2.27 in /usr/local/lib/python3.10/dist-packages (from libpysal) (2.32.3) Requirement already satisfied: scipy>=1.8 in /usr/local/lib/python3.10/dist-packages (from libpysal) (1.13.1) Requirement already satisfied: shapely>=2.0.1 in /usr/local/lib/python3.10/dist-packages (from libpysal) (2.0.6) Requirement already satisfied: scikit-learn>=1.1 in /usr/local/lib/python3.10/dist-packages (from libpysal) (1.5.2) Collecting giddy (from splot) Downloading giddy-2.3.5-py3-none-any.whl.metadata (6.4 kB) Requirement already satisfied: mapclassify in /usr/local/lib/python3.10/dist-packages (from splot) (2.8.1) Requirement already satisfied: matplotlib>=3.3.3 in /usr/local/lib/python3.10/dist-packages (from splot) (3.8.0) Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.10/dist-packages (from splot) (0.13.2) Collecting spreg (from splot) Downloading spreg-1.8-py3-none-any.whl.metadata (1.7 kB) Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4>=4.10->libpysal) (2.6) Requirement already satisfied: pyogrio>=0.7.2 in /usr/local/lib/python3.10/dist-packages (from geopandas>=0.10.0->libpysal) (0.10.0) Requirement already satisfied: pyproj>=3.3.0 in /usr/local/lib/python3.10/dist-packages (from geopandas>=0.10.0->libpysal) (3.7.0) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (1.3.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (4.55.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (1.4.7) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (11.0.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (3.2.0) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.3->splot) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.4->libpysal) (2024.2) Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.4->libpysal) (2024.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.27->libpysal) (3.4.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.27->libpysal) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.27->libpysal) (2.2.3) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.27->libpysal) (2024.8.30) Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.1->libpysal) (1.4.2) Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.1->libpysal) (3.5.0) Collecting quantecon>=0.4.7 (from giddy->splot) Downloading quantecon-0.7.2-py3-none-any.whl.metadata (4.9 kB) Requirement already satisfied: networkx>=2.7 in /usr/local/lib/python3.10/dist-packages (from mapclassify->splot) (3.4.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib>=3.3.3->splot) (1.16.0) Requirement already satisfied: numba>=0.49.0 in /usr/local/lib/python3.10/dist-packages (from quantecon>=0.4.7->giddy->splot) (0.60.0) Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from quantecon>=0.4.7->giddy->splot) (1.13.1) Requirement already satisfied: llvmlite<0.44,>=0.43.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.49.0->quantecon>=0.4.7->giddy->splot) (0.43.0) Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->quantecon>=0.4.7->giddy->splot) (1.3.0) Downloading libpysal-4.12.1-py3-none-any.whl (2.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.8/2.8 MB 25.7 MB/s eta 0:00:00 Downloading esda-2.6.0-py3-none-any.whl (135 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 135.4/135.4 kB 10.0 MB/s eta 0:00:00 Downloading splot-1.1.7-py3-none-any.whl (39 kB) Downloading giddy-2.3.5-py3-none-any.whl (61 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.1/61.1 kB 4.5 MB/s eta 0:00:00 Downloading spreg-1.8-py3-none-any.whl (379 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 379.8/379.8 kB 23.8 MB/s eta 0:00:00 Downloading quantecon-0.7.2-py3-none-any.whl (215 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 215.4/215.4 kB 15.7 MB/s eta 0:00:00 Installing collected packages: quantecon, libpysal, spreg, esda, giddy, splot Successfully installed esda-2.6.0 giddy-2.3.5 libpysal-4.12.1 quantecon-0.7.2 splot-1.1.7 spreg-1.8
# Cargar el shapefile o geopackage con los datos espaciales
datadisMap = gpd.read_file(
"https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej2.gpkg"
)
datadisMap.head()
| name | kind | latitude_deg | longitude_deg | elevation_ft | iso_region | municipality | geometry | |
|---|---|---|---|---|---|---|---|---|
| 0 | New Ishigaki Airport | medium_airport | 24.396389 | 124.245000 | 102.0 | JP-47 | Ishigaki | POINT (24849.797 -177601.73) |
| 1 | JMSDF Chichijima Airfield | seaplane_base | 27.090231 | 142.192032 | NaN | JP-13 | Ogasawara | POINT (1821915.408 255473.101) |
| 2 | Urayasu Heliport | heliport | 35.623658 | 139.894506 | 16.0 | JP-12 | Urayasu | POINT (1445601.261 1185419.126) |
| 3 | Yagishiri Emergency Heliport | heliport | 44.439286 | 141.400487 | NaN | JP-01 | Haboro | POINT (1385302.891 2195212.393) |
| 4 | Yokohama Heliport | heliport | 35.342222 | 139.656389 | 4.0 | JP-14 | Yokohama | POINT (1428917.497 1150234.837) |
datadisMap.dtypes
| 0 | |
|---|---|
| name | object |
| kind | object |
| latitude_deg | float64 |
| longitude_deg | float64 |
| elevation_ft | float64 |
| iso_region | object |
| municipality | object |
| geometry | geometry |
# Para observar la cantidad de variables numericas
numeric_columns = datadisMap.select_dtypes(include=['float64', 'int64']).columns
print(f"Variables numéricas en el dataset: {list(numeric_columns)}")
Variables numéricas en el dataset: ['latitude_deg', 'longitude_deg', 'elevation_ft']
# Verificar las nuevas variables
print(datadisMap[['latitude_deg', 'longitude_deg', 'elevation_ft']].head())
latitude_deg longitude_deg elevation_ft 0 24.396389 124.245000 102.0 1 27.090231 142.192032 NaN 2 35.623658 139.894506 16.0 3 44.439286 141.400487 NaN 4 35.342222 139.656389 4.0
# Seleccionar una de las variables numéricas
variable = "latitude_deg"
# Crear matriz de vecinos usando KNN (K=8)
k = 8
w_knn8 = KNN.from_dataframe(datadisMap, k=k)
/usr/local/lib/python3.10/dist-packages/libpysal/weights/distance.py:153: UserWarning: The weights matrix is not fully connected: There are 2 disconnected components. W.__init__(self, neighbors, id_order=ids, **kwargs)
# Transformar los pesos para que sumen 1
w_knn8.transform = "R"
from libpysal.weights.distance import KNN
# Calcular el coeficiente de Moran para la variable seleccionada
moran = Moran(datadisMap[variable], w_knn8)
print(f"Coeficiente de Moran: {moran.I}")
print(f"Valor p-simulado: {moran.p_sim}")
Coeficiente de Moran: 0.9916672561677757 Valor p-simulado: 0.001
# Crear el diagrama de dispersión de Moran
fig, ax = moran_scatterplot(moran, aspect_equal=True)
ax.set_xlabel(f"{variable}_std")
ax.set_ylabel(f"SpatialLag_{variable}_std")
ax.set_title(f"Diagrama de Dispersión de Moran - {variable}")
plt.show()